﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 管理员添加好友到群聊
	/// </summary>
	public class Action30081 : BaseAction
	{
		private int talkingTableId;
		/// <summary>
		/// 好友列表string     List<int>
		/// </summary>
		private string userIds;
		private bool result = false;

		public Action30081(HttpGet actionGetter)
			: base((short)ActionType.DZAddFriendToTableTalk, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TalkingTableId", ref talkingTableId) &&
				httpGet.GetString("UserIds", ref userIds))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var table = GameTalkingTable.Current.Find(talkingTableId);
			if (table == null)
			{
				Tool.WriteWarmingLine("消息30081群聊组不存在");
				return false;
			}
			var ids = ProtoBufUtils.Deserialize<List<int>>(Convert.FromBase64String(userIds));
			if (ids.Count <= 0)
			{
				Tool.WriteWarmingLine("消息30081没有发送好友ID");
				return false;
			}

			var EffectUsersId = new List<int>();
		    foreach(var id in ids)
			{
				var gameUser = GameTable.Current.GetUser(id);
				if (gameUser != null)
					EffectUsersId.Add(id);
			}

			if (!table.AddRangeIntoTable(EffectUsersId))
			{
				Tool.WriteWarmingLine("消息30081邀请好友到群聊失败");
				return false;
			}
			this.result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.talkingTableId);
			this.PushIntoStack(this.result);
		}
	}
}
